#!/bin/bash
# -------------------------------
# Filename:    test.sh
# Revision:    1.0
# Date:        2013-08-01
# Author:      simonzhang
# Email:       simon-zzm@163.com
# Description: 
# -------------------------------

Raspberrypi()
{
    echo "raspberry or pcduino(ubutnu os)"
    sudo apt-get install -y subversion
    sudo apt-get install -y git-core
    sudo apt-get install -y zip sed rsync
    sudo easy_install tornado
    sudo easy_install PyCrypto
    sudo apt-get install -y python-MySQLdb
    sudo easy_install torndb
    sudo easy_install pexpect
    sudo sysctl -n -w fs.inotify.max_user_watches=16384
}


CentOS()
{
    echo "C"
    sudo yum install -y subversion
    sudo yum install -y git-core 
    sudo yum install -y zip sed rsync
    sudo yum install -y mysql-*
    sudo easy_install tornado
    sudo easy_install PyCrypto
    sudo yum install -y python-MySQLdb
    sudo easy_install torndb
    sudo easy_install pexpect
    sudo sysctl -n -w fs.inotify.max_user_watches=16384
}

function randnum()
{
    di=(0 1 2 3 4 5 6 7 8 9 \
        a b c d e f g h i j k l m n o p q r s t u v w x y z \
        A B C D E F G H I G K L M N O P Q R S T U V W X Y Z \
        ~ ! ^ _)
    for(( i=0; i<$1; i++))
    do
        num=$num`echo -n ${di[$RANDOM % ${#di[*]}]}`
    done
    echo $num
}

#base info 
echo -e "\033[31m MySql IP \033[0m"
read MySqlIp
echo -e "\033[31m MySql Port \033[0m"
read MySqlPort
echo -e "\033[31m MySql DB Name \033[0m"
read MySqlDB
echo -e "\033[31m MySql User \033[0m"
read MySqlUser
echo -e "\033[31m MySql password \033[0m"
read MySqlPasswd
echo -e "\033[31m title \033[0m"
read title
echo -e "\033[31m Domain Name \033[0m"
read DomainNanme
echo -e "\033[31m local global ip \033[0m"
read localip
echo -e "\033[31m local global port \033[0m"
read localport
echo -e "\033[31m===================\033[0m"
# must package
echo -e "\033[31m Please input your system id
     id   system name
     1    CentOS
     2    Raspberry or PcDuino(ubutnu os)
plase write system id:\033[0m"
read sys_id

# start install base package
case ${sys_id} in
    1)
        CentOS
        ;;
    2)
        Raspberrypi
       ;;
    *)
       echo "Not find yout system id"
       exit 1
esac

# main file
rm -rf ../main.sh
cp ../main.sh.bak ../main.sh
sleep 2
sed -i 's/globalipxxx/'${localip}'/g' ../main.sh
sed -i 's/globalportxxx/'${localport}'/g' ../main.sh
chmod 755 ../main.sh

# config file
rm -rf ../config.py
cp ../config.py.bak ../config.py
sleep 2
ran_45=$(randnum 45)
sed -i 's%cookiexxx%'${ran_45}='%g' ../config.py
ran_8=$(randnum 8)
sed -i 's%secpassxxx%'${ran_8}'%g' ../config.py
sed -i 's/mysqlipxxx/'${MySqlIp}'/g' ../config.py
sed -i 's/mysqlportxxx/'${MySqlPort}'/g' ../config.py
sed -i 's/mysqluserxxx/'${MySqlUser}'/g' ../config.py
sed -i 's/mysqlpassxxx/'${MySqlPasswd}'/g' ../config.py
sed -i 's/mysqldbxxx/'${MySqlDB}'/g' ../config.py

# mysql script
rm -rf ../deploy.sql
cp ../deploy.sql.bak ../deploy.sql
sleep 2
sed -i 's/titlexxx/'${title}'/g' ../deploy.sql
sed -i 's/domainnamexxx/'${DomainNanme}'/g' ../deploy.sql

# start run sql
mysql -u$MySqlUser -p$MySqlPasswd -h$MySqlIp -P$MySqlPort $MySqlDB<../deploy.sql

# install end
cd ..
bash main.sh start
echo "install ok.Placse use admin default password 1 access http://"${DomainNanme}"/"
